package userQuery

import (
	"zhou_gin_api/app/dao"
	"zhou_gin_api/app/input"
	"zhou_gin_api/app/models"
	"zhou_gin_api/app/output/code"
	"zhou_gin_api/app/output/userOutput"
)

type userQueryService struct {
}

func NewUserQueryService() *userQueryService {
	return &userQueryService{}
}

func (service *userQueryService) Index(userInput input.IndexUserInput) []userOutput.IndexUserOutput {
	userDao := dao.NewUserDao()

	var userModel models.UserModel
	userModel.Username = userInput.Username
	userModel.Age = userInput.Age
	userModel.ID = userInput.Id

	userModels := userDao.Gets(&userModel)

	userOutputs := make([]userOutput.IndexUserOutput, 0)
	for _, value := range userModels {
		var tempUserOutput userOutput.IndexUserOutput

		tempUserOutput.Id = value.ID
		tempUserOutput.Username = value.Username
		tempUserOutput.Age = value.Age
		tempUserOutput.CreatedAt = value.CreatedAt

		userOutputs = append(userOutputs, tempUserOutput)
	}

	return userOutputs
}

func (service *userQueryService) Show(id uint) (user *models.UserModel, errCode string, msg string) {

	userDao := dao.NewUserDao()

	user, err := userDao.First(id)
	if err != nil {
		if user.Age == 0 {
			return nil, code.ResponseCode["USER_NOT_EXIST"], ""
		}
		return nil, code.ResponseCode["EXCEPTION_MESSAGE"], err.Error()
	}

	return user, code.ResponseCode["SUCCESS"], ""
}
